package com.ruoyi.coupons.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.coupons.domain.MemberCoupon;
import com.ruoyi.coupons.service.MemberCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/memberCoupon")
public class MemberCouponController extends BaseController {

    @Autowired
    private MemberCouponService memberCouponService;
    /**
     * 查询每个用户符合状态的所有优惠券
     */
    @GetMapping("/list")
    public TableDataInfo getList(int memberId,int couponStatus){

        List<MemberCoupon> list = memberCouponService.getList(memberId,couponStatus);
        return getDataTable(list);
    }

    /**
     * 查询优惠券个数
     */
    @GetMapping("/num")
    public AjaxResult getNum(int memberId){

        int number=memberCouponService.getNum(memberId);

        if (number<0){
            return error("没有该用户");
        }
        System.out.println(number);
        return success(number);
    }


    /**
     *用户领取优惠券
     */
    @PostMapping("/lingqu")
    public AjaxResult lingqu(@RequestParam("couponId") int couponId,@RequestParam("memberId") int memberId,@RequestParam(required = false, defaultValue = "1") int requestCount){
        System.out.println(requestCount);
        System.out.println(memberId);
        System.out.println(couponId);
        try {
            MemberCoupon memberCoupon = memberCouponService.lingqu(couponId, memberId, requestCount);
            return AjaxResult.success("领取成功", memberCoupon); // 返回数据
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
